﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using TCHSHue.DAL;
using TCHSHue.BLL;
namespace TCHSHue.Modules.IndexControl.HoiDap
{
    public partial class ctrHoiDap : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int pCurrent = getPageCurrent();
                DanhSachCacCauHoiDaDuocTraLoi(pCurrent);
                LoadCaptcha();
            }
        }

        void DanhSachCacCauHoiDaDuocTraLoi(int pCurrent)
        {
            var ls = HoiDapBLL.getListTheoTrang(Hang.PAGESIZE, pCurrent,(int) TrangThaiHoiDap.DaDuyet);
            ddlHoiDap.DataSource = ls;
            ddlHoiDap.DataBind();
            var tsBai = HoiDapBLL.getToTal((int)TrangThaiHoiDap.DaDuyet);
            PhanTrang(tsBai, pCurrent, "../Modules/Index.aspx?mode=hoidap&");
        }

        void LoadCaptcha()
        {
            Session["captcha"] = new CaptchaBLL().CreateCaptchaText();
            imgCaptcha.ImageUrl = "../../CaptchaHandler.ashx";
        }

        protected void bntGoi_Click(object sender, EventArgs e)
        {
            thongBao.Visible = true;
            if (string.IsNullOrEmpty(txtHoTen.Text))
                lbThongBao.Text = "Bạn chưa nhập họ tên";
            else if (string.IsNullOrEmpty(txtChuDe.Text))
                lbThongBao.Text = "Bạn chưa nhập chủ đề muốn hỏi";
            else if (string.IsNullOrEmpty(txtNoiDung.Text))
                lbThongBao.Text = "Bạn chưa nhập nội dung muốn hỏi";
            else
            {
                string captchaInput = txtCaptcha.Text;
                if (captchaInput != (string)Session["captcha"])
                {
                    lbThongBao.Text = "Mã xác nhận nhập không chính xác";
                    txtCaptcha.Focus();
                    return;
                }
                var hoTen = Server.HtmlEncode(txtHoTen.Text);
                var diaChiThuongTru = Server.HtmlEncode(txtDiaChi.Text);
                var email = Server.HtmlEncode(txtEmail.Text);
                var soDienThoai = Server.HtmlEncode(txtSDT.Text);
                var chuDe = Server.HtmlEncode(txtChuDe.Text);
                var noiDungHoi = Server.HtmlEncode(txtNoiDung.Text);
                DAL.HoiDap hd = new DAL.HoiDap() { maHD = Guid.NewGuid(), hoTenNguoiHoi = hoTen, diaChiNguoiHoi = diaChiThuongTru, email = email, soDienThoai = soDienThoai, chuDe = chuDe, noiDungCauHoi = noiDungHoi, ngayHoi = DateTime.Now.Date, trangThai = (int)TrangThaiHoiDap.ChuaDuyet };
                if (HoiDapBLL.create(hd))
                {
                    lbThongBao.Text = "Đã gởi câu hỏi thành công";
                    txtChuDe.Text = "";
                    txtDiaChi.Text = "";
                    txtEmail.Text = "";
                    txtHoTen.Text = "";
                    txtNoiDung.Text = "";
                    txtSDT.Text = "";
                    txtCaptcha.Text = "";
                    txtHoTen.Focus();
                }
                else
                    lbThongBao.Text = "Gởi câu hỏi thất bại! Xin vui lòng thử lại";
                LoadCaptcha();
            }
        }

        int getPageCurrent()
        {
            var pageIndex = Request.QueryString["pageIndex"];
            int temp;
            int pCurrent = 1;
            if (!int.TryParse(pageIndex, out temp))
                pCurrent = 1;
            else
                pCurrent = int.Parse(pageIndex);
            return pCurrent;
        }

        void PhanTrang(int total, int pCurrent, string baseLink)
        {
            Paging paGing = new Paging(Hang.PAGESIZE);
            string resultPaging = paGing.CreatePaging(total, pCurrent, baseLink);
            if (string.IsNullOrEmpty(resultPaging))
                divPaging.Visible = false;
            else
            {
                divPaging.InnerHtml = resultPaging;
            }
        }

        protected void bntTimKiem_Click(object sender, EventArgs e)
        {
            int pCurrent = getPageCurrent();
            var ls = HoiDapBLL.getListTheoTuKhoa(txtTimKiem.Text, Hang.PAGESIZE, pCurrent);
            ddlHoiDap.DataSource = ls;
            ddlHoiDap.DataBind();
            var tsBai = HoiDapBLL.getToTal(txtTimKiem.Text);
            PhanTrang(tsBai, pCurrent, "../Modules/Index.aspx?mode=hoidap&");
        }
    }
}